package com.ruoyi.school.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.school.domain.PacClass;
import com.ruoyi.school.domain.PacClassTeacher;
import com.ruoyi.school.domain.PacTeacher;
import com.ruoyi.school.domain.Student;
import com.ruoyi.school.domain.dto.ImportTeacherInfo;
import com.ruoyi.school.enums.RoleTypeEnum;
import com.ruoyi.school.service.PacClassService;
import com.ruoyi.school.service.PacClassTeacherService;
import com.ruoyi.school.service.PacTeacherService;
import com.ruoyi.school.mapper.PacTeacherMapper;
import com.ruoyi.school.service.StudentService;
import com.ruoyi.school.utils.CommonUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author jeremylin
 * @description 针对表【pac_teacher(教师表)】的数据库操作Service实现
 * @createDate 2025-05-27 02:52:40
 */
@Service
public class PacTeacherServiceImpl extends ServiceImpl<PacTeacherMapper, PacTeacher>
        implements PacTeacherService {

    @Resource
    private PacClassService pacClassService;

    @Resource
    private ISysUserService iSysUserService;

    @Resource
    private PacClassTeacherService pacClassTeacherService;

    @Resource
    private StudentService studentService;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysRoleService sysRoleService;

    /**
     * 获得我的老师的信息
     * 1、获得我的班级
     * 2、通过班级找到老师
     *
     * @return
     */
    @Override
    public SysUser getMyTeacherInfo() {
        Student student = CommonUtils.getStudent();
        if (student != null) {
            Long classId = student.getClassId();
            PacClass pacClass = pacClassService.getById(classId);
            if (pacClass != null) {
                Integer bindTeacher = pacClass.getBindTeacher();
                if (bindTeacher != null) {
                    SysUser sysUser = iSysUserService.selectUserById(bindTeacher.longValue());
                    return sysUser;
                }
            }
        }
        return null;
    }

    /**
     * 根据ids 获取老师信息
     *
     * @param teacherIds
     * @return
     */
    @Override
    public List<PacTeacher> getTeacherListByIds(List<Integer> teacherIds) {
        if (teacherIds != null) {
            return lambdaQuery()
                    .in(PacTeacher::getId, teacherIds)
                    .list();
        }
        return new ArrayList<>();
    }

    @Override
    public List<PacTeacher> getTeacherListByUserIds(List<Integer> teacherIds) {
        if (teacherIds != null) {
            return lambdaQuery()
                    .in(PacTeacher::getUserId, teacherIds)
                    .list();
        }
        return new ArrayList<>();
    }

    /**
     * 获取工号获取老师信息
     *
     * @param teacherNum
     * @return
     */
    @Override
    public PacTeacher getOneByTeaNum(String teacherNum) {
        if (teacherNum == null) {
            return null;
        }
        return lambdaQuery()
                .eq(PacTeacher::getTeacherNum, teacherNum)
                .one();
    }

    /**
     * 根据用户id查询
     *
     * @param userId
     * @return
     */
    @Override
    public PacTeacher getByUserId(Long userId) {
        if (userId != null) {
            return lambdaQuery()
                    .eq(PacTeacher::getUserId, userId)
                    .one();
        }
        return null;
    }

    /**
     * 根据用户id删除老师
     *
     * @param userIds
     */
    @Override
    public void deleteTeacherByUserIds(Long[] userIds) {
        lambdaUpdate()
                .in(PacTeacher::getUserId, userIds)
                .remove();
    }

    /**
     * 通过班级id，找到学业老师
     *
     * @param classId
     * @return
     */
    @Override
    public PacTeacher getAcademicTeacherInfo(Long classId) {
        List<PacClassTeacher> listByClassId = pacClassTeacherService.getListByClassId(classId.intValue());
        for (PacClassTeacher pacClassTeacher : listByClassId) {
            if (Objects.equals(pacClassTeacher.getRoleKey(), RoleTypeEnum.TEACHER.getName())) {
                return lambdaQuery().eq(PacTeacher::getId, pacClassTeacher.getTeacherId()).one();
            }
        }
        return null;
    }

    /**
     * 获取学生的所有老师
     *
     * @param stuId
     * @return
     */
    @Override
    public List<PacTeacher> getStuAllTeacher(Integer stuId) {
        // 找到学生的班级
        Student byId = studentService.getById(stuId);
        if (byId != null) {
            List<PacClassTeacher> listByClassId = pacClassTeacherService.getListByClassId(byId.getClassId().intValue());
            if (listByClassId.isEmpty()) return new ArrayList<>();
            List<Integer> teacherIds = listByClassId.stream().map(PacClassTeacher::getTeacherId).collect(Collectors.toList());
            return lambdaQuery().in(PacTeacher::getId, teacherIds).list();
        }
        return new ArrayList<>();
    }

    @Override
    public List<ImportTeacherInfo> selectList() {
        List<PacTeacher> list = lambdaQuery().list();
        List<ImportTeacherInfo> infoList = new ArrayList<>();
        List<Long> userIds = list.stream().map(PacTeacher::getUserId).map(Long::valueOf).collect(Collectors.toList());
        List<SysUser> sysUsers = sysUserService.selectUserListByIds(userIds);
        Map<Long, SysUser> userMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
        list.forEach(item -> {
            List<SysRole> sysRoles = sysRoleService.selectRolesByUserId(item.getUserId().longValue());
            List<Long> collect = sysRoles.stream().map(SysRole::getRoleId).collect(Collectors.toList());

            ImportTeacherInfo info = new ImportTeacherInfo();
            SysUser sysUser = userMap.get(item.getUserId().longValue());
            info.setTeacherName(item.getTeacherName());
            info.setTeacherNum(item.getTeacherNum());
            info.setBirth(item.getBirth());
            info.setSex(sysUser.getSex());
            info.setTitle(item.getTitle());
            info.setEducation(item.getEducation());
            info.setDegree(item.getDegree());
            info.setFaculties(item.getFaculties());
            info.setPhone(sysUser.getPhonenumber());
            String teacherTypeName = "";
            if (collect.contains(104L)) {
                teacherTypeName = "学业导师";
                if (collect.contains(105L)) {
                    teacherTypeName += "、专业导师";
                }
            } else if (collect.contains(105L)) {
                teacherTypeName = "专业导师";
            }

            info.setTeacherType(teacherTypeName);
            infoList.add(info);
        });
        return infoList;
    }

    /**
     * @param teacherName
     * @return
     */
    @Override
    public Long getIdByName(String teacherName) {
        return 0L;
    }
}




